package com.shop.mall.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
 
/**
 * md5Util工具类
 * @author admin
 *
 */
public class MD5Utils {
	
    /**
     * Used to build output as Hex
     */
    private static final char[] DIGITS_LOWER =
        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * Used to build output as Hex
     */
    private static final char[] DIGITS_UPPER =
        {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 
	/**
	 * 普通加密01
	 */
	public static String getStrMD5(String inStr) {
		// 获取MD5实例
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			System.out.println(e.toString());
			return "";
		}
 
		// 将加密字符串转换为字符数组
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];
 
		// 开始加密
		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] digest = md5.digest(byteArray);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < digest.length; i++) {
			int var = digest[i] & 0xff;
			if (var < 16)
				sb.append("0");
			sb.append(Integer.toHexString(var));
		}
		return sb.toString();
	}
 
	/**
	 * 普通MD5加密 02
	 */
	public static String getStrrMD5(String content) {
 
		try {
			byte strTemp[] = content.getBytes("UTF-8");
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte md[] = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = DIGITS_LOWER[byte0 >>> 4 & 15];
				str[k++] = DIGITS_LOWER[byte0 & 15];
			}
 
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
 
	/**
	 * MD5双重解密
	 */
	public static String getconvertMD5(String inStr) {
		char[] charArray = inStr.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			charArray[i] = (char) (charArray[i] ^ 't');
		}
		String str = String.valueOf(charArray);
		return str;
	}
 
	/**
	 * Hex类实现Hex(16进制字符串和)和字节数组的互转
	 */
	private static String md5Hex(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] digest = md.digest(str.getBytes());
			return new String(encodeHex(digest));
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
			return "";
		}
	}
	
 
	private static char[] encodeHex(byte[] data) {
		final int l = data.length;
        final char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return out;
	}

	/**
	 * 加盐MD5加密
	 */
	public static String getSaltMD5(String content) {
		// 生成一个16位的随机数
		Random random = new Random();
		StringBuilder sBuilder = new StringBuilder(16);
		sBuilder.append(random.nextInt(99999999)).append(random.nextInt(99999999));
		int len = sBuilder.length();
		if (len < 16) {
			for (int i = 0; i < 16 - len; i++) {
				sBuilder.append("0");
			}
		}
		// 生成最终的加密盐
		String Salt = sBuilder.toString();
		content = md5Hex(content + Salt);
		char[] cs = new char[48];
		for (int i = 0; i < 48; i += 3) {
			cs[i] = content.charAt(i / 3 * 2);
			char c = Salt.charAt(i / 3);
			cs[i + 1] = c;
			cs[i + 2] = content.charAt(i / 3 * 2 + 1);
		}
		return String.valueOf(cs);
	}
 
	/**
	 * 验证加盐后是否和原文一致
	 */
	public static boolean getSaltverifyMD5(String content, String md5str) {
		char[] cs1 = new char[32];
		char[] cs2 = new char[16];
		for (int i = 0; i < 48; i += 3) {
			cs1[i / 3 * 2] = md5str.charAt(i);
			cs1[i / 3 * 2 + 1] = md5str.charAt(i + 2);
			cs2[i / 3] = md5str.charAt(i + 1);
		}
		String Salt = new String(cs2);
		return md5Hex(content + Salt).equals(String.valueOf(cs1));
	}
	public static void main(String[] args) {
		MD5Utils md = new MD5Utils();
		String strMD5 = new String("这是一个测试加密的数据");
 
		System.out.println("原始：" + strMD5);
		System.out.println("MD5原方法：" + md.getStrrMD5(strMD5));
		System.out.println("MD5修改方法：" + md.getStrMD5(strMD5));
		System.out.println("加密的：" + md.getconvertMD5(strMD5));
		System.out.println("解密的：" + md.getconvertMD5(md.getconvertMD5(strMD5)));
 
		System.out.println("\t\t=======================================");
		// 原文
		String plaintext = "这是一个测试普通MD5和加盐MD5的数据是否相同";
		// plaintext = "123456";
		System.out.println("原始：" + plaintext);
		System.out.println("普通MD5后：" + MD5Utils.getStrMD5(plaintext));
 
		// 获取加盐后的MD5值
		String ciphertext = MD5Utils.getSaltMD5(plaintext);
		System.out.println("加盐后MD5：" + ciphertext);
		System.out.println("是否是同一字符串:" + MD5Utils.getSaltverifyMD5(plaintext, ciphertext));
 
	}
 
}
